<template>
  <div>
    <div class="newaddress_box">
      <div class="newaddress_head">
        <span class="iconfont icon-jiantou-left" @click="$router.go(-1)"></span>
        <p>添加新地址</p>
      </div>
      <div class="newaddress">
        <div class="box">
          <p>收货人</p>
          <input type="text" placeholder="请填写收货人姓名" v-model="name" />
        </div>
        <div class="box">
          <p>手机号码</p>
          <input type="text" placeholder="请填写手机号码" v-model="pohone" />
        </div>
        <div class="box">
          <p>地址</p>
          <input type="text" placeholder="请填写收货地址" v-model="address" />
        </div>
      </div>
      <div class="add_btn_box">
        <div class="btn" @click="add">保存</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data: function () {
    return {
      name: "",
      pohone: "",
      address: "",
    };
  },
  created() {
    //判断是否是修改
    let index = this.$route.query.index;
    if (index) {
      let addresslists = JSON.parse(sessionStorage.getItem("address"));
      let address = addresslists[index];
      this.name = address.name;
      this.pohone = address.pohone;
      this.address = address.home;
    }
  },
  methods: {
    //保存地址
    add() {
      //判断是否是修改
      let index = this.$route.query.index;
      if (index) {
        let addresslists = JSON.parse(sessionStorage.getItem("address"));
        addresslists[index].name = this.name;
        addresslists[index].pohone = this.pohone;
        addresslists[index].home = this.address;
        sessionStorage.setItem("address", JSON.stringify(addresslists));
      } else {
        let addresslists = sessionStorage.getItem("address");
        if (!addresslists) {
          sessionStorage.setItem("address", "");
          let lists = [];
          let address = {
            name: this.name,
            pohone: this.pohone,
            home: this.address,
          };
          lists.push(address);
          console.log(lists);
          sessionStorage.setItem("address", JSON.stringify(lists));
        } else {
          addresslists = JSON.parse(addresslists);
          let address = {
            name: this.name,
            pohone: this.pohone,
            home: this.address,
          };
          addresslists.push(address);
          sessionStorage.setItem("address", JSON.stringify(addresslists));
        }
      }
      this.$router.push("/address");
    },
  },
};
</script>

<style lang="less">
.newaddress_box {
  .newaddress_head {
    padding: 10px 18px;
    background-color: #fff;
    display: flex;
    align-items: center;
    position: sticky;
    top: 0;
    left: 0;
    span {
      font-weight: bold;
    }
    p {
      display: inline-block;
      padding-left: 33.6%;
    }
  }
  .newaddress {
    padding: 5px 20px;
    background-color: #fff;
    .box {
      display: flex;
      padding: 10px 0;
      border-bottom: 1px solid #cccad0;
      p {
        display: inline-block;
        width: 60px;
        font-size: 14px;
        font-weight: bold;
      }
      input {
        margin-left: 30px;
        border: 0;
        :-ms-input-placeholder {
          color: #cccad0;
        }
      }
    }
  }
  .add_btn_box {
    padding: 150px 10px 30px;
    background-color: #fff;
    .btn {
      width: 100%;
      height: 40px;
      background-color: #00c2c2;
      line-height: 40px;
      color: #fff;
      text-align: center;
    }
  }
}
</style>